package com.bsoft.exchange.service;

import com.bsoft.exchange.domain.*;
import com.bsoft.exchange.pojo.CheckReportParam;
import com.bsoft.exchange.pojo.InspectionListParam;
import com.bsoft.exchange.util.*;
import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;


import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.*;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 检验检查报告接口
 */
@Service
public class InspectionReportService {

    private final Gson gson = new Gson();

    public String getDoctorName(String code, String url) {
        OutputStream out = null;
        InputStream input = null;
        String name = null;
        try {
            HttpURLConnection httpURLConnection = getHttpURLConnection(url);
            String requestBody = ParamXmlUtils.format(ParamXmlUtils.codeParam(code), "GetYgxm");
            out = httpURLConnection.getOutputStream();
            out.write(requestBody.getBytes());
            int statusCode = httpURLConnection.getResponseCode();
            if (statusCode == 200) {
                input = httpURLConnection.getInputStream();
                byte[] b = new byte[1024];
                StringBuilder result = new StringBuilder();
                int length = 0;
                while ((length = input.read(b)) != -1) {
                    String str = new String(b, 0, length, StandardCharsets.UTF_8);
                    result.append(str);
                }
                String xml = Dom4jUtil.xmlToStr(result.toString());
                if (StringUtils.isNoneBlank(xml)) {
                    JsonObject jsonObject = gson.fromJson(xml, JsonObject.class);
                    JsonArray jsonArray = jsonObject.get("row").getAsJsonArray();
                    if (jsonArray.size() > 0) {
                        JsonObject jsonObject1 = gson.fromJson(jsonArray.get(0), JsonObject.class);
                        name = jsonObject1.get("YGXM").getAsString();
                    }
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            releaseResources(out, input);
        }
        return name;
    }

    /**
     * 根据身份证号码查询患者信息
     *
     * @param url    接口地址
     * @param idCard 身份证号码
     * @return 患者信息列表
     */
    public BaseResponse<List<PatientSearchInfo>> getRecord(String url, String idCard) {
        OutputStream out = null;
        InputStream input = null;
        BaseResponse<List<PatientSearchInfo>> baseResponse = new BaseResponse<>();
        List<PatientSearchInfo> patientSearchInfos = new ArrayList<>();
        try {
            HttpURLConnection httpURLConnection = getHttpURLConnection(url);
            String requestBody = ParamXmlUtils.format(ParamXmlUtils.idCardParam(idCard), "GetPatientBySfzh");
            out = httpURLConnection.getOutputStream();
            out.write(requestBody.getBytes());
            int code = httpURLConnection.getResponseCode();
            if (code == 200) {
                input = httpURLConnection.getInputStream();
                byte[] b = new byte[1024];
                StringBuilder result = new StringBuilder();
                int length = 0;
                while ((length = input.read(b)) != -1) {
                    String str = new String(b, 0, length, StandardCharsets.UTF_8);
                    result.append(str);
                }
                String xml = Dom4jUtil.xmlToStr(result.toString());
                if (StringUtils.isBlank(xml)) {
                    baseResponse.setMessage("未查询到对应患者信息！");
                } else {
                    JsonObject jsonObject = gson.fromJson(xml, JsonObject.class);
                    JsonArray jsonArray = jsonObject.get("row").getAsJsonArray();
                    for (JsonElement jsonElement : jsonArray) {
                        PatientSearchInfo patientSearchInfo = gson.fromJson(jsonElement, PatientSearchInfo.class);
                        patientSearchInfos.add(patientSearchInfo);
                    }
                    baseResponse.setMessage("查询成功！");
                    baseResponse.setData(patientSearchInfos);
                }
            } else {
                baseResponse.setMessage("未查询到对应患者信息！");
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            releaseResources(out, input);
        }
        baseResponse.setCode(Constant.successResponse);
        return baseResponse;
    }

    /**
     * 根据患者类型，机构代码，门（急）诊号查询检验报告列表
     *
     * @param url                接口地址
     * @param patientType        患者类型
     * @param authorOrganization 机构代码
     * @param clinicId           门（急）诊号
     * @return 检验报告列表
     */
    public BaseResponse<List<InspectionListDomain>> getInspectionList(String url, String patientType, String authorOrganization, String clinicId, String reportDate) {
        OutputStream out = null;
        InputStream input = null;
        BaseResponse<List<InspectionListDomain>> baseResponse = new BaseResponse<>();
        List<InspectionListDomain> inspectionListDomains = new ArrayList<>();
        try {
            HttpURLConnection httpURLConnection = getHttpURLConnection(url);
            String requestBody = ParamXmlUtils.format(ParamXmlUtils.inspectionListParam(patientType, authorOrganization, clinicId, reportDate), "GetLabReportListByPatId");
            out = httpURLConnection.getOutputStream();
            out.write(requestBody.getBytes());
            int code = httpURLConnection.getResponseCode();
            if (code == 200) {
                input = httpURLConnection.getInputStream();
                byte[] b = new byte[1024];
                StringBuilder result = new StringBuilder();
                int length = 0;
                while ((length = input.read(b)) != -1) {
                    String str = new String(b, 0, length, StandardCharsets.UTF_8);
                    result.append(str);
                }
                String replace = StringUtils.replace(result.toString(), "&lt;", "<");
                String msg = StringUtils.replace(replace, "&gt;", ">");
                String s = Dom4jUtil.xmlToStr(msg);
                if (StringUtils.isBlank(s)) {
                    baseResponse.setMessage("未查询到对应患者检验报告列表信息！");
                } else {
                    List<String> labReportList = Dom4jUtil.getResult(s, "LabReportList");
                    for (String str : labReportList) {
                        InspectionListDomain inspectionListDomain = JaxbUtil.xmlToBean(str, InspectionListDomain.class);

                        if (inspectionListDomain != null) {
                            String reportDateTime1 = inspectionListDomain.getReportDateTime();
                            String reportReviewTime1 = inspectionListDomain.getReportReviewTime();
                            Date reportDateTime = DateUtil.StrToFormatDate(reportDateTime1, "yyyyMMdd'T'hhmmss");
                            String reportDateTimeStr = DateUtil.dateFormat(reportDateTime);
                            Date reportReviewTime = DateUtil.StrToFormatDate(reportReviewTime1, "yyyyMMdd'T'hhmmss");
                            String reportReviewTimeStr = DateUtil.dateFormat(reportReviewTime);
                            inspectionListDomain.setReportDateTime(reportDateTimeStr);
                            inspectionListDomain.setReportReviewTime(reportReviewTimeStr);
                        }
                        inspectionListDomains.add(inspectionListDomain);
                    }
                    baseResponse.setMessage("查询成功！");
                    baseResponse.setData(inspectionListDomains);
                }
            } else {
                baseResponse.setMessage("未查询到对应患者检验报告列表信息！");
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            releaseResources(out, input);
        }
        baseResponse.setCode(Constant.successResponse);
        return baseResponse;
    }

    /**
     * 检验报告详情查询-常规报告
     *
     * @param url                接口地址
     * @param sampleNo           检验标本号
     * @param barcodeNo          条码号
     * @param authorOrganization 就诊机构代码
     * @return 常规报告
     */
    public BaseResponse<RegularReportDomain> getRegularReport(String url, String sampleNo, String barcodeNo, String authorOrganization) {
        OutputStream out = null;
        InputStream input = null;
        BaseResponse<RegularReportDomain> baseResponse = new BaseResponse<>();
        RegularReportDomain regularReportDomain = null;
        try {
            HttpURLConnection httpURLConnection = getHttpURLConnection(url);
            String requestBody = ParamXmlUtils.format(ParamXmlUtils.regularReportParam(sampleNo, barcodeNo, authorOrganization), "GetLabReportById_Com");
            out = httpURLConnection.getOutputStream();
            out.write(requestBody.getBytes());
            int code = httpURLConnection.getResponseCode();
            if (code == 200) {
                input = httpURLConnection.getInputStream();
                byte[] b = new byte[1024];
                StringBuilder result = new StringBuilder();
                int length = 0;
                while ((length = input.read(b)) != -1) {
                    String str = new String(b, 0, length, StandardCharsets.UTF_8);
                    result.append(str);
                }
                String replace = StringUtils.replace(result.toString(), "&lt;", "<");
                String msg = StringUtils.replace(replace, "&gt;", ">");
                String s = Dom4jUtil.xmlToStr(msg);
                if (StringUtils.isBlank(s)) {
                    baseResponse.setMessage("未查询到对应患者检验常规报告列表信息！");
                }
                List<String> labReport = Dom4jUtil.getResult(s, "LabReport");
                if (labReport.size() < 1) {
                    baseResponse.setMessage("未查询到对应患者检验常规报告列表信息！");
                } else {
                    String labReportStr = labReport.get(0);
                    regularReportDomain = JaxbUtil.xmlToBean(labReportStr, RegularReportDomain.class);
                    fomateDate(regularReportDomain);
                    regularReportDomain.setReportDoctorName(getDoctorName(regularReportDomain.getReportDoctor(), url));
                    regularReportDomain.setRequestDoctorName(getDoctorName(regularReportDomain.getRequestDoctor(), url));
                    regularReportDomain.setReportReviewDoctorName(getDoctorName(regularReportDomain.getReportReviewDoctor(), url));
                    baseResponse.setData(regularReportDomain);
                    baseResponse.setMessage("查询成功！");
                }
            } else {
                baseResponse.setMessage("未查询到对应患者检验常规报告列表信息！");
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            releaseResources(out, input);
        }
        baseResponse.setCode(Constant.successResponse);
        return baseResponse;
    }

    /**
     * 常规报告日期格式化
     *
     * @param regularReportDomain 常规报告实体
     */
    private void fomateDate(RegularReportDomain regularReportDomain) {
        if (regularReportDomain != null) {
            String reportDateTime = regularReportDomain.getReportDateTime();
            Date reportDateTime1 = DateUtil.StrToFormatDate(reportDateTime, "yyyyMMdd'T'hhmmss");
            String reportDateTimeStr = DateUtil.dateFormat(reportDateTime1);
            String reportReviewTime = regularReportDomain.getReportReviewTime();
            Date reportReviewTime1 = DateUtil.StrToFormatDate(reportReviewTime, "yyyyMMdd'T'hhmmss");
            String reportReviewTimeStr = DateUtil.dateFormat(reportReviewTime1);
            String requestTime = regularReportDomain.getRequestTime();
            Date requestTime1 = DateUtil.StrToFormatDate(requestTime, "yyyyMMdd'T'hhmmss");
            String requestTimeStr = DateUtil.dateFormat(requestTime1);
            String sampleExecuteTime = regularReportDomain.getSampleExecuteTime();
            Date sampleExecuteTime1 = DateUtil.StrToFormatDate(sampleExecuteTime, "yyyyMMdd'T'hhmmss");
            String sampleExecuteTimeStr = DateUtil.dateFormat(sampleExecuteTime1);
            String sampleReceiveTime = regularReportDomain.getSampleReceiveTime();
            Date sampleReceiveTime1 = DateUtil.StrToFormatDate(sampleReceiveTime, "yyyyMMdd'T'hhmmss");
            String sampleReceiveTimeStr = DateUtil.dateFormat(sampleReceiveTime1);
            regularReportDomain.setReportDateTime(reportDateTimeStr);
            regularReportDomain.setReportReviewTime(reportReviewTimeStr);
            regularReportDomain.setRequestTime(requestTimeStr);
            regularReportDomain.setSampleExecuteTime(sampleExecuteTimeStr);
            regularReportDomain.setSampleReceiveTime(sampleReceiveTimeStr);
        }
    }


    /**
     * 检验报告详情查询-微生物
     *
     * @param url                接口地址
     * @param sampleNo           检验标本号
     * @param barcodeNo          条码号
     * @param authorOrganization 就诊机构代码
     * @return 检验报告详情
     */
    public BaseResponse<MicrobiologyReportDomain> getMicrobiologyReport(String url, String sampleNo, String barcodeNo, String authorOrganization) {
        OutputStream out = null;
        InputStream input = null;
        BaseResponse<MicrobiologyReportDomain> baseResponse = new BaseResponse<>();
        MicrobiologyReportDomain microbiologyReportDomain = null;
        try {
            HttpURLConnection httpURLConnection = getHttpURLConnection(url);
            String requestBody = ParamXmlUtils.format(ParamXmlUtils.regularReportParam(sampleNo, barcodeNo, authorOrganization), "GetLabReportById_Anti");
            out = httpURLConnection.getOutputStream();
            out.write(requestBody.getBytes());
            int code = httpURLConnection.getResponseCode();
            if (code == 200) {
                input = httpURLConnection.getInputStream();
                byte[] b = new byte[1024];
                StringBuilder result = new StringBuilder();
                int length = 0;
                while ((length = input.read(b)) != -1) {
                    String str = new String(b, 0, length, StandardCharsets.UTF_8);
                    result.append(str);
                }
                String replace = StringUtils.replace(result.toString(), "&lt;", "<");
                String msg = StringUtils.replace(replace, "&gt;", ">");
                String s = Dom4jUtil.xmlToStr(msg);
                if (StringUtils.isBlank(s)) {
                    baseResponse.setMessage("未查询到对应患者检验常规报告列表信息！");
                }
                List<String> labReport = Dom4jUtil.getResult(s, "LabReport");
                if (labReport.size() < 1) {
                    baseResponse.setMessage("未查询到对应患者检验常规报告列表信息！");
                } else {
                    String labReportStr = labReport.get(0);
                    microbiologyReportDomain = JaxbUtil.xmlToBean(labReportStr, MicrobiologyReportDomain.class);
                    fomateDate2(microbiologyReportDomain);
                    microbiologyReportDomain.setRequestDoctorName(getDoctorName(microbiologyReportDomain.getRequestDoctor(), url));
                    microbiologyReportDomain.setReportDoctorName(getDoctorName(microbiologyReportDomain.getReportDoctor(), url));
                    microbiologyReportDomain.setReportReviewDoctorName(getDoctorName(microbiologyReportDomain.getReportReviewDoctor(), url));
                    baseResponse.setData(microbiologyReportDomain);
                    baseResponse.setMessage("查询成功！");
                }
            } else {
                baseResponse.setMessage("未查询到对应患者检验常规报告列表信息！");
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            releaseResources(out, input);
        }
        baseResponse.setCode(Constant.successResponse);
        return baseResponse;
    }

    /**
     * 格式化微生物检验报告日期格式
     *
     * @param microbiologyReportDomain 报告实体
     */
    private void fomateDate2(MicrobiologyReportDomain microbiologyReportDomain) {
        if (microbiologyReportDomain != null) {
            String reportDateTime = microbiologyReportDomain.getReportDateTime();
            Date reportDateTime1 = DateUtil.StrToFormatDate(reportDateTime, "yyyyMMdd'T'hhmmss");
            String reportDateTimeStr = DateUtil.dateFormat(reportDateTime1);
            String reportReviewTime = microbiologyReportDomain.getReportReviewTime();
            Date reportReviewTime1 = DateUtil.StrToFormatDate(reportReviewTime, "yyyyMMdd'T'hhmmss");
            String reportReviewTimeStr = DateUtil.dateFormat(reportReviewTime1);
            String requestTime = microbiologyReportDomain.getRequestTime();
            Date requestTime1 = DateUtil.StrToFormatDate(requestTime, "yyyyMMdd'T'hhmmss");
            String requestTimeStr = DateUtil.dateFormat(requestTime1);
            String sampleExecuteTime = microbiologyReportDomain.getSampleExecuteTime();
            Date sampleExecuteTime1 = DateUtil.StrToFormatDate(sampleExecuteTime, "yyyyMMdd'T'hhmmss");
            String sampleExecuteTimeStr = DateUtil.dateFormat(sampleExecuteTime1);
            String sampleReceiveTime = microbiologyReportDomain.getSampleReceiveTime();
            Date sampleReceiveTime1 = DateUtil.StrToFormatDate(sampleReceiveTime, "yyyyMMdd'T'hhmmss");
            String sampleReceiveTimeStr = DateUtil.dateFormat(sampleReceiveTime1);
            microbiologyReportDomain.setReportDateTime(reportDateTimeStr);
            microbiologyReportDomain.setReportReviewTime(reportReviewTimeStr);
            microbiologyReportDomain.setRequestTime(requestTimeStr);
            microbiologyReportDomain.setSampleExecuteTime(sampleExecuteTimeStr);
            microbiologyReportDomain.setSampleReceiveTime(sampleReceiveTimeStr);
        }
    }

    /**
     * 检查报告列表查询
     *
     * @param url                接口地址
     * @param patientType        患者类型
     * @param authorOrganization 就诊机构代码
     * @param clinicId           门（急）诊号
     * @return 检查报告列表
     */
    public BaseResponse<List<ExamReportListDomain>> getExamReportList(String url, String patientType, String authorOrganization, String clinicId, String reportDate) {
        OutputStream out = null;
        InputStream input = null;
        BaseResponse<List<ExamReportListDomain>> baseResponse = new BaseResponse<>();
        List<ExamReportListDomain> examReportListDomains = new ArrayList<>();
        try {
            HttpURLConnection httpURLConnection = getHttpURLConnection(url);
            String requestBody = ParamXmlUtils.format(ParamXmlUtils.ExamListParam(authorOrganization, clinicId, reportDate), "GetExamReportListByPatId");
            out = httpURLConnection.getOutputStream();
            out.write(requestBody.getBytes());
            int code = httpURLConnection.getResponseCode();
            if (code == 200) {
                input = httpURLConnection.getInputStream();
                byte[] b = new byte[1024];
                StringBuilder result = new StringBuilder();
                int length = 0;
                while ((length = input.read(b)) != -1) {
                    String str = new String(b, 0, length, StandardCharsets.UTF_8);
                    result.append(str);
                }
                String replace = StringUtils.replace(result.toString(), "&lt;", "<");
                String msg = StringUtils.replace(replace, "&gt;", ">");
                String s = Dom4jUtil.xmlToStr(msg);
                if (StringUtils.isBlank(s)) {
                    baseResponse.setMessage("未查询到对应患者检验报告列表信息！");
                }
                List<String> labReportList = Dom4jUtil.getResult(s, "ExamReportList");
                for (String str : labReportList) {
                    ExamReportListDomain examReportListDomain = JaxbUtil.xmlToBean(str, ExamReportListDomain.class);
                    if (examReportListDomain != null) {
                        String reportDateTime = examReportListDomain.getReportDateTime();
                        Date date = DateUtil.StrToFormatDate(reportDateTime, "yyyyMMdd'T'hhmmss");
                        reportDateTime = DateUtil.dateFormat(date);
                        examReportListDomain.setReportDateTime(reportDateTime);
                    }
                    examReportListDomains.add(examReportListDomain);
                }
                baseResponse.setMessage("查询成功！");
                baseResponse.setData(examReportListDomains);
            } else {
                baseResponse.setMessage("未查询到对应患者检验报告列表信息！");
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            releaseResources(out, input);
        }
        baseResponse.setCode(Constant.successResponse);
        return baseResponse;
    }

    /**
     * 检查报告详情查询
     *
     * @param url                接口地址
     * @param reportId           报告单编号
     * @param authorOrganization 就诊机构代码
     * @return 检查报告详情
     */
    public BaseResponse<ExamReportDomain> getExamReport(String url, String reportId, String authorOrganization) {
        OutputStream out = null;
        InputStream input = null;
        BaseResponse<ExamReportDomain> baseResponse = new BaseResponse<>();
        try {
            HttpURLConnection httpURLConnection = getHttpURLConnection(url);
            String requestBody = ParamXmlUtils.format(ParamXmlUtils.examReportParam(reportId, authorOrganization), "GetExamReportById");
            out = httpURLConnection.getOutputStream();
            out.write(requestBody.getBytes());
            int code = httpURLConnection.getResponseCode();
            if (code == 200) {
                input = httpURLConnection.getInputStream();
                byte[] b = new byte[1024];
                StringBuilder result = new StringBuilder();
                int length = 0;
                while ((length = input.read(b)) != -1) {
                    String str = new String(b, 0, length, StandardCharsets.UTF_8);
                    result.append(str);
                }
                String replace = StringUtils.replace(result.toString(), "&lt;", "<");
                String msg = StringUtils.replace(replace, "&gt;", ">");
                String s = Dom4jUtil.xmlToStr(msg);
                if (StringUtils.isBlank(s)) {
                    baseResponse.setMessage("未查询到对应患者检验报告列表信息！");
                }
                List<String> examReport = Dom4jUtil.getResult(s, "ExamReport");
                if (examReport.size() < 1) {
                    baseResponse.setMessage("未查询到对应患者检验报告列表信息！");
                } else {
                    String examReportXml = examReport.get(0);
                    ExamReportDomain examReportDomain = JaxbUtil.xmlToBean(examReportXml, ExamReportDomain.class);
                    if (examReportDomain != null) {
                        String reportDateTime = examReportDomain.getReportDateTime();
                        if (StringUtils.isNotBlank(reportDateTime) && reportDateTime.length() == 15) {
                            Date date = DateUtil.StrToFormatDate(reportDateTime, "yyyyMMdd'T'hhmmss");
                            reportDateTime = DateUtil.dateFormat(date);
                            examReportDomain.setReportDateTime(reportDateTime);
                        } else {
                            Date date = DateUtil.StrToFormatDate(reportDateTime, "yyyyMMdd");
                            reportDateTime = DateUtil.dateFormat(date);
                            examReportDomain.setReportDateTime(reportDateTime);
                        }
                        String executeDateTime = examReportDomain.getExecuteDateTime();
                        if (StringUtils.isNotBlank(executeDateTime) && executeDateTime.length() == 15) {
                            Date date1 = DateUtil.StrToFormatDate(executeDateTime, "yyyyMMdd'T'hhmmss");
                            executeDateTime = DateUtil.dateFormat(date1);
                            examReportDomain.setExecuteDateTime(executeDateTime);
                        }
                        String reviewDateTime = examReportDomain.getReviewDateTime();
                        if (StringUtils.isNotBlank(reviewDateTime) && reviewDateTime.length() == 15) {
                            Date date2 = DateUtil.StrToFormatDate(reviewDateTime, "yyyyMMdd'T'hhmmss");
                            reviewDateTime = DateUtil.dateFormat(date2);
                            examReportDomain.setReviewDateTime(reviewDateTime);
                        }

                    }
                    baseResponse.setMessage("查询成功！");
                    baseResponse.setData(examReportDomain);
                }
            } else {
                baseResponse.setMessage("未查询到对应患者检验报告列表信息！");
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            releaseResources(out, input);
        }
        baseResponse.setCode(Constant.successResponse);
        return baseResponse;
    }

    private HttpURLConnection getHttpURLConnection(String url) throws IOException {
        URL url1 = new URL(url);
        URLConnection urlConnection = url1.openConnection();
        HttpURLConnection httpURLConnection = (HttpURLConnection) urlConnection;
        httpURLConnection.setDoInput(true);
        httpURLConnection.setDoOutput(true);
        httpURLConnection.setRequestMethod("POST");
        httpURLConnection.setRequestProperty("content-type", "text/xml;charset=UTF-8");
        return httpURLConnection;
    }

    private void releaseResources(OutputStream out, InputStream input) {
        if (out != null) {
            try {
                out.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        if (input != null) {
            try {
                input.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    public ListInspectionReportResponse inspectionList(InspectionListParam inspectionListParam, String url) {
        String reportDate = inspectionListParam.getReportDate();
        String organizationId = inspectionListParam.getAuthorOrganizationId();
        String cardNumber = inspectionListParam.getCardNumber();
        List<InspectionListDomain> result = new ArrayList<>();
        //先根据身份证查询患者信息
        BaseResponse<List<PatientSearchInfo>> record = getRecord(url, cardNumber);
        List<PatientSearchInfo> data = record.getData();
        if (data != null && data.size() > 1) {
            PatientSearchInfo patientSearchInfo = data.get(0);
            BaseResponse<List<InspectionListDomain>> baseResponse = getInspectionList(url, patientSearchInfo.getPatientType(), organizationId, patientSearchInfo.getPatientId(), reportDate);
            List<InspectionListDomain> inspectionListDomains = baseResponse.getData();
            if (inspectionListDomains != null) {
                for (InspectionListDomain inspectionListDomain : inspectionListDomains) {
                    BaseResponse<RegularReportDomain> regularReport = getRegularReport(url, inspectionListDomain.getSampleNo(), inspectionListDomain.getBarcodeNo(), inspectionListDomain.getAuthorOrganization());
                    BaseResponse<MicrobiologyReportDomain> microbiologyReport = getMicrobiologyReport(url, inspectionListDomain.getSampleNo(), inspectionListDomain.getBarcodeNo(), inspectionListDomain.getAuthorOrganization());
                    inspectionListDomain.setRegularReportDomain(regularReport.getData());
                    inspectionListDomain.setMicrobiologyReportDomain(microbiologyReport.getData());
                }
                result.addAll(inspectionListDomains);
            }
        }
        ListInspectionReportResponse listInspectionReportResponse = new ListInspectionReportResponse();
        listInspectionReportResponse.setTotal((long) result.size());
        listInspectionReportResponse.setList(page(result, inspectionListParam.getPageNo(), inspectionListParam.getPageSize()));
        return listInspectionReportResponse;
    }

    public ListExamReportResponse listExamReport(CheckReportParam checkReportParam, String url) {
        String reportDate = checkReportParam.getReportDate();
        String organizationId = checkReportParam.getHospitalCode();
        String cardNumber = checkReportParam.getPatientIdentityCardNumber();
        List<ExamReportListDomain> examReportListDomains = new ArrayList<>();
        //先根据身份证查询患者信息
        BaseResponse<List<PatientSearchInfo>> record = getRecord(url, cardNumber);
        List<PatientSearchInfo> data = record.getData();
        if (data != null && data.size() > 1) {
            PatientSearchInfo patientSearchInfo = data.get(0);
            BaseResponse<List<ExamReportListDomain>> baseResponse = getExamReportList(url, patientSearchInfo.getPatientType(), organizationId, patientSearchInfo.getPatientId(), reportDate);
            List<ExamReportListDomain> examReportList = baseResponse.getData();
            if (examReportList != null) {
                for (ExamReportListDomain examReportListDomain : examReportList) {
                    BaseResponse<ExamReportDomain> examReport = getExamReport(url, examReportListDomain.getReportId(), examReportListDomain.getAuthorOrganization());
                    ExamReportDomain examReportDomain = examReport.getData();
                    examReportListDomain.setExamReportDomain(examReportDomain);
                }
                examReportListDomains.addAll(examReportList);
            }
        }
        ListExamReportResponse listExamReportResponse = new ListExamReportResponse();
        listExamReportResponse.setTotal((long) examReportListDomains.size());
        listExamReportResponse.setExamReportListDomains(page(examReportListDomains, checkReportParam.getPageNo(), checkReportParam.getPageSize()));
        return listExamReportResponse;
    }

    public List page(List list, int pageNo, int pageSize) {
        int total = list.size();
        int pages = total % pageSize == 0 ? total / pageSize : total / pageSize + 1;
        if (pageNo > pages || pageNo < 1) {
            return new ArrayList<>();
        }
        int start = (pageNo - 1) * pageSize;
        int end = start + pageSize;
        if (end > total) {
            end = total;
        }
        return list.subList(start, end);

    }


    public static void main(String[] args) {
        InspectionReportService inspectionReportService = new InspectionReportService();
//        InspectionListParam inspectionListParam = new InspectionListParam();
//        inspectionListParam.setPageSize(5);
//        inspectionListParam.setPageNo(1);
//        inspectionListParam.setCardNumber("320902199003075639");
//        inspectionListParam.setAuthorOrganizationId("46821120-9");
//        ListInspectionReportResponse listInspectionReportResponse = inspectionReportService.inspectionList(inspectionListParam, "http://192.168.160.218:9010/wx?wsdl");
//        System.out.println(listInspectionReportResponse);
        CheckReportParam checkReportParam = new CheckReportParam();
        checkReportParam.setHospitalCode("46821120-9");
        checkReportParam.setPatientIdentityCardNumber("320902199003075639");
        checkReportParam.setPageNo(1);
        checkReportParam.setPageSize(10);
        ListExamReportResponse listExamReportResponse = inspectionReportService.listExamReport(checkReportParam, "http://192.168.160.218:9010/wx?wsdl");
        System.out.println(listExamReportResponse);
    }
}
